package com.xydtech.task;

import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.ecard.config.domain.EcardNewsChannelList;
import com.xydtech.ecard.config.domain.NewsContentRequest;
import com.xydtech.ecard.config.service.IEcardApiSysConfigService;
import com.xydtech.ecard.config.service.IEcardNewsChannelListService;
import com.xydtech.ecard.config.service.IEcardNewsContentListService;
import com.xydtech.ecard.custgroup.domain.EcardApiCustomerOrganizeTask;
import com.xydtech.ecard.custgroup.domain.EcardApiCustomerOrganizeVisit;
import com.xydtech.ecard.custgroup.service.IEcardApiCustomerOrganizeTaskService;
import com.xydtech.ecard.custgroup.service.IEcardApiCustomerOrganizeVisitService;
import com.xydtech.ecard.ecard.domain.EcardApiMerchantReservationTask;
import com.xydtech.ecard.ecard.domain.EcardApiMerchantTask;
import com.xydtech.ecard.ecard.domain.EcardApiMerchantVisitInfo;
import com.xydtech.ecard.ecard.service.IEcardApiMerchantReservationTaskService;
import com.xydtech.ecard.ecard.service.IEcardApiMerchantTaskService;
import com.xydtech.ecard.ecard.service.IEcardApiMerchantVisitInfoService;
import com.xydtech.system.domain.SysConfig;
import com.xydtech.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 定时任务调度测试
 *
 * @author xydtech
 */
@Component("eCardTask")
public class ECardTask {
    @Autowired
    private IEcardNewsContentListService ecardNewsContentListService;
    @Autowired
    private IEcardNewsChannelListService ecardNewsChannelListService;
    @Autowired
    private IEcardApiMerchantVisitInfoService ecardMerchantVisitInfoService;
    @Autowired
    private IEcardApiMerchantTaskService ecardApiMerchantTaskService;
    @Autowired
    private IEcardApiSysConfigService ecardApiSysConfigService;
    @Autowired
    private IEcardApiMerchantReservationTaskService ecardApiMerchantReservationTaskService;
    @Autowired
    private IEcardApiCustomerOrganizeVisitService ecardCustomerOrganizeVisitService;
    @Autowired
    private IEcardApiCustomerOrganizeTaskService ecardCustomerOrganizeTaskService;
    @Autowired
    private ISysDeptService sysDeptService;


    /**
     * 更新新闻列表
     *
     * @param
     * @return void
     * @throws
     * @Title: updateNewsContentList
     * @author JHON
     * @Date 2022/8/18 10:41
     **/
    public void updateNewsContentList() {
        EcardNewsChannelList ecardNewsChannelList = new EcardNewsChannelList();
        ecardNewsChannelList.setSts("1");
        ecardNewsChannelList.setUpdateWay("1");
        List<EcardNewsChannelList> channelList = ecardNewsChannelListService.selectEcardNewsChannelListList(ecardNewsChannelList);
        for (EcardNewsChannelList channel : channelList) {
            NewsContentRequest newsContentRequest = new NewsContentRequest();
            newsContentRequest.setChannelId(channel.getChannelId());
            newsContentRequest.setDeptId(channel.getDeptId());
            newsContentRequest.setPage(1);
            newsContentRequest.setMaxResult(3);
            ecardNewsContentListService.downLoadNewsContentList(newsContentRequest);
        }

    }

    /***
     * @description: 电子名片-网格任务-个人/商户/企业/新型农业主体任务
     * @author yjs
     * @date: 2023/7/31 11:40
     */
    public void addMerchantTasks() {

        //定时任务-查询最近记录且回访日期为7天前的数据
        /*List<EcardApiMerchantVisitInfo> visitList = ecardMerchantVisitInfoService.getMerchantLastVisitList(sysConfig.getConfigValue())*/;
        EcardApiMerchantVisitInfo ecardApiMerchantVisitInfo = new EcardApiMerchantVisitInfo();
        List<EcardApiMerchantVisitInfo> visitList = ecardMerchantVisitInfoService.getAllVisitList(ecardApiMerchantVisitInfo);
        for (EcardApiMerchantVisitInfo visitInfo : visitList) {
            if (visitInfo.getReminderDate() != null) {
                List<EcardApiMerchantVisitInfo> merchantVisitList = ecardMerchantVisitInfoService.getMerchantVisitList(visitInfo);
                //由于参数配置改为多法人，在这获取不到登录人的机构id，所以用商户的机构id去查参数，然后循环判断
                SysConfig config = new SysConfig();
                config.setConfigKey("ecard_merchant_task_days");
                SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(merchantVisitList.get(0).getDeptId());
                if (sysDept != null) {
                    config.setDeptId(sysDept.getDeptId());
                }
                SysConfig sysConfig = ecardApiSysConfigService.selectConfig(config);
                int taskDays = Integer.valueOf(sysConfig.getConfigValue());//拜访任务定时天数
                //计算下次回访日期和当前日期相差天数
                int millisecond = DateUtils.differentDaysByActualMillisecond(new Date(), merchantVisitList.get(0).getReminderDate());
                if ((millisecond + 1) == taskDays) {
                    EcardApiMerchantTask merchantTask = new EcardApiMerchantTask();
                    merchantTask.setMerchantId(merchantVisitList.get(0).getMerchantId());
                    merchantTask.setCmRegistId(merchantVisitList.get(0).getManagementPersonnelId());
                    merchantTask.setEndDate(merchantVisitList.get(0).getReminderDate());
                    merchantTask.setMerchantType(merchantVisitList.get(0).getMerchantType());//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
                    merchantTask.setTaskSts("0");
                    ecardApiMerchantTaskService.insertEcardMerchantTask(merchantTask);
                }
            }
        }
    }

    /**
     * 电子名片-网格任务-社团组织任务
     *
     * @author yuzhiguo
     * @date 2024/12/2 下午3:03
     */
    public void addOrganizeTasks() {
        EcardApiCustomerOrganizeVisit ecardApiCustomerOrganizeVisit = new EcardApiCustomerOrganizeVisit();
        List<EcardApiCustomerOrganizeVisit> organizeVisitList = ecardCustomerOrganizeVisitService.getAllVisitList(ecardApiCustomerOrganizeVisit);
        for (EcardApiCustomerOrganizeVisit organizeVisit : organizeVisitList) {
            if (organizeVisit.getReminderDate() != null) {
                List<EcardApiCustomerOrganizeVisit> organizeVisits = ecardCustomerOrganizeVisitService.getOrganizeVisitList(organizeVisit);
                //由于参数配置改为多法人，在这获取不到登录人的机构id，所以用社团组织的机构id去查参数，然后循环判断
                SysConfig config = new SysConfig();
                config.setConfigKey("ecard_merchant_task_days");
                SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(organizeVisits.get(0).getDeptId());
                if (sysDept != null) {
                    config.setDeptId(sysDept.getDeptId());
                }
                SysConfig sysConfig = ecardApiSysConfigService.selectConfig(config);
                int taskDays = Integer.valueOf(sysConfig.getConfigValue());//拜访任务定时天数
                //计算下次回访日期和当前日期相差天数
                int millisecond = DateUtils.differentDaysByActualMillisecond(new Date(), organizeVisits.get(0).getReminderDate());
                if ((millisecond + 1) == taskDays) {
                    EcardApiCustomerOrganizeTask organizeTask = new EcardApiCustomerOrganizeTask();
                    organizeTask.setOrganizeId(organizeVisits.get(0).getId());
                    organizeTask.setCmRegistId(organizeVisits.get(0).getManagementPersonnelId());
                    organizeTask.setDeptId(organizeVisits.get(0).getDeptId());
                    organizeTask.setCorpId(organizeVisits.get(0).getCorpId());
                    organizeTask.setEndDate(organizeVisits.get(0).getReminderDate());
                    organizeTask.setTaskSts("0");
                    ecardCustomerOrganizeTaskService.insertEcardCustomerOrganizeTask(organizeTask);
                }
            }
        }
    }

    /***
     *  电子名片 定时跑 个人/商户/企业/新型农业主体预约任务
     * @param
     * @return: void
     * @Author: lin
     * @Date: 2023/8/5 11:31
     */
    public void addMerchantReservationTasks(){

        //查询最近记录且回访日期为days天前的数据
        //List<EcardApiMerchantVisitInfo> visitList = ecardMerchantVisitInfoService.getMerchantReservationList(sysConfig.getConfigValue());
        EcardApiMerchantVisitInfo ecardApiMerchantVisitInfo = new EcardApiMerchantVisitInfo();
        List<EcardApiMerchantVisitInfo> visitList = ecardMerchantVisitInfoService.getAllVisitList(ecardApiMerchantVisitInfo);
        for (EcardApiMerchantVisitInfo visitInfo : visitList) {
            if (visitInfo.getAppointmentDate() != null) {
                List<EcardApiMerchantVisitInfo> merchantVisitList = ecardMerchantVisitInfoService.getMerchantVisitList(visitInfo);
                //由于参数配置改为多法人，在这获取不到登录人的机构id，所以用商户的机构id去查参数，然后循环判断
                SysConfig config = new SysConfig();
                config.setConfigKey("ecard_merchant_reservation_task_days");
                SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(merchantVisitList.get(0).getDeptId());
                if (sysDept != null) {
                    config.setDeptId(sysDept.getDeptId());
                }
                SysConfig sysConfig = ecardApiSysConfigService.selectConfig(config);
                int taskDays = Integer.valueOf(sysConfig.getConfigValue());//拜访任务定时天数
                //计算下次回访日期和当前日期相差天数
                int millisecond = DateUtils.differentDaysByActualMillisecond(new Date(), merchantVisitList.get(0).getAppointmentDate());
                if ((millisecond + 1) == taskDays) {
                    EcardApiMerchantReservationTask ecardApiMerchantReservationTask = new EcardApiMerchantReservationTask();
                    ecardApiMerchantReservationTask.setMerchantId(merchantVisitList.get(0).getMerchantId());
                    ecardApiMerchantReservationTask.setCmRegistId(merchantVisitList.get(0).getManagementPersonnelId());
                    ecardApiMerchantReservationTask.setAppointmentDate(merchantVisitList.get(0).getAppointmentDate());
                    ecardApiMerchantReservationTask.setReservationBusiType(merchantVisitList.get(0).getReservationBusiType());
                    ecardApiMerchantReservationTask.setMerchantType(merchantVisitList.get(0).getMerchantType());//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
                    ecardApiMerchantReservationTask.setTaskSts("0");
                    ecardApiMerchantReservationTaskService.insertEcardMerchantReservationTask(ecardApiMerchantReservationTask);
                }
            }
        }
    }

}
